﻿//创建人：Wangjh
//日期：2021-12-15
//描述：叶片领料流程
var page = {
    pageType: getParam("type"), //type:1工艺确认，2温度填写，3检验确认,4到料确认
    processNode: '',
    pagetotal: 0,
    pageSize: 30,
    pageNumber: 1,
    rootPath: getRootPath(), //网站根路径
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页

        //根据页面类型，显示页面元素
        if (page.pageType == "1") {
            page.processNode = '工艺确认'
            $("#btnChangeWorkflow").css("display", "inline-block");
        } else if (page.pageType == "2") {
            page.processNode = '温度确认'
            $("#btnTemperature").css("display", "inline-block");
        } else if (page.pageType == "3") {
            page.processNode = '物料检验'
            $("#dvtxtDPH").css("display", "block");
        } else if (page.pageType == "4") {
            page.processNode = '到料确认'
            $("#dvSDS").css("display", "block");
        }
    },
    //页面事件绑定
    bind: function () {
        //主查询结果加载
        $("#gdContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            onClickRow: function (index) {//add:Wangjh 20211125
                page.searchDataDetail();
            },
            columns: [[
		         { field: 'PRODUCTBATCH', title: '件号/批次号', width: 130 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 110 },
                 { field: 'WIPQTY', title: '在制', width: 40 },
                 { field: 'SUMFACTORYSTARTQTY', title: '计划', width: 40 },
		         { field: 'WORKFLOWINFO', title: '工艺', width: 140 },
                 { field: 'WORKFLOWSTEPNAME', title: '当前节点', width: 100 },
                 { field: 'CONTAINERTYPENAME', title: '流水卡类型', width: 100 },
                 { field: 'USEQUOTA', title: '消耗定额', width: 55 },
                 { field: 'TOGETHERFLAG', title: '批次顺序', hidden: true },
                 { field: 'CONTAINERTYPE', title: '流水卡类型', hidden: true },
                 { field: 'PRODUCTID', title: '零件图号ID', hidden: true },
                 { field: 'PRODUCTNAME', title: '零件图号', hidden: true },
                 { field: 'FACTORYNAME', title: '分厂', hidden: true },
                 { field: 'CONTAINERLEVELNAME', title: '批次层次', hidden: true },
                 { field: 'WORKFLOWSTEPID', title: '工序ID', hidden: true },
                 { field: 'ISHOLD', title: 'IsHold', hidden: true },
                 { field: 'ISLASTSTEP', title: 'islaststep', hidden: true },
                 { field: 'MFGWORKFLOWID', title: 'mfgworkflowid', hidden: true },
                 { field: 'SEQUENCE', title: 'sequence', hidden: true },
                 { field: 'BEGINSTEP', title: 'BeginStep', hidden: true },
                 { field: 'CHILDCONTAINERSQL', title: 'sonSql', hidden: true },
                 { field: 'PRODUCTBATCHHIDDEN', title: 'ProductBatchHidden', hidden: true },
                 { field: 'PRODUCTTYPENAME', title: '零件类型', hidden: true },
                 { field: 'WORKFLOWROUTE', title: '父零件号', hidden: true },
                 { field: 'CURWORKFLOWINFO', title: '当前工艺', hidden: true },
                 { field: 'MATERIALTRADEMARK', title: 'MATERIALTRADEMARK', hidden: true }
	             ]]
        });
        //批次号列表加载
        $("#gdContainerInfo2").datagrid({
            onLoadSuccess: function (data) {
                if (data.rows.length > 0) {
                    $(this).datagrid('checkAll');
                    $(this).datagrid('selectRow', 0);
                }
            },
            onSelect: function (rowIndex, rowData) {
                page.searchMaterialChildData(rowData);
                //文本框信息展示
                $("#txtDPH").textbox('setValue', rowData.YPCONTAINERNO);
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
		         { field: 'CONTAINERNAME', title: '流水卡号', width: 130 },
                 { field: 'SFS', title: '实发数/顺序号', width: 100 },
                 { field: 'RLHZ', title: '熔炉号子', width: 100 },
                 { field: 'RLHM', title: '熔炉号母', width: 100 },
		         { field: 'JYBH', title: '检验编号', width: 100 },
                 { field: 'DLPH', title: '代料牌号', width: 100 },
                 { field: 'DLGG', title: '代料规格', width: 100 },
                 { field: 'DLDH', title: '代料单号', width: 100 },
                 { field: 'PXZYGX', title: '平行作业工序', width: 100 },
                 { field: 'PXZYDH', title: '平行作业单号', width: 100 },
                 { field: 'CONTAINERID', title: 'CONTAINERID', hidden: true },
                 { field: 'CONTAINERTYPE', title: '流水卡类型', hidden: true },
                 { field: 'ISSELFMFG', title: 'ISSELFMFG', hidden: true },
                 { field: 'BEGINSTEP', title: 'BEGINSTEP', hidden: true },
                 { field: 'CLOSECONTIANER', title: 'CLOSECONTIANER', hidden: true },
                 { field: 'CONTAINERLEVELNAME', title: '批次层次', hidden: true },
                 { field: 'WORKFLOWSTEPID', title: '工序ID', hidden: true },
                 { field: 'ISHOLD', title: 'IsHold', hidden: true },
                 { field: 'ISLASTSTEP', title: 'islaststep', hidden: true },
                 { field: 'MFGWORKFLOWID', title: 'mfgworkflowid', hidden: true },
                 { field: 'SEQUENCE', title: 'sequence', hidden: true },
                 { field: 'BEGINSTEP', title: 'BeginStep', hidden: true },
                 { field: 'CHILDCONTAINERSQL', title: 'sonSql', hidden: true },
                 { field: 'PRODUCTBATCHHIDDEN', title: 'ProductBatchHidden', hidden: true },
                 { field: 'PRODUCTTYPENAME', title: '零件类型', hidden: true },
                 { field: 'WORKFLOWROUTE', title: '父零件号', hidden: true },
                 { field: 'CURWORKFLOWINFO', title: '当前工艺', hidden: true },
                 { field: 'ORGCONTAINERNAME', title: 'ORGCONTAINERNAME', hidden: true },
                 { field: 'YPCONTAINERTYPE', title: 'ypcontainertype', hidden: true },
                 { field: 'ISTITANIUMMATERIAL', title: '是否钛合金', hidden: true },
                 { field: 'ISHAVETEMPERA', title: '是否维护温度', hidden: true }

	             ]]
        });
        //子批次
        $("#gdChildContainer").datagrid({
            onLoadSuccess: function (data) {

            },
            columns: [[
		         { field: 'CONTAINERNAME', title: '子流水卡号', width: 150 },
                 { field: 'SFS', title: '实发数/顺序号', width: 100 },
                 { field: 'RLH', title: '熔炉号', width: 100 }
	             ]]
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.searchData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.searchData();
        });
        //查询按钮事件
        $('#btnSearch').on("click", function () {
            page.pageNumber = 1;
            page.searchData();
        });
        $('#btnReset').on("click", function () {
            page.reset(true, true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
        //扫描批次事件
        $('#txtScanContainer').textbox({
            onChange: function (n, o) {
                page.searchData();
            }
        });
        //确认按钮事件
        $('#btnConfirm').on("click", function () {
            page.confirmData();
        });
        //查看流水卡事件
        $('#btnViewPrint').on("click", function () {
            page.viewContainerPrint();
        });
        //修改工艺事件
        $('#btnChangeWorkflow').on("click", function () {
            page.changeWorkflow();
        });
        //数量变更
        $('#btnChangeQty').on("click", function () {
            page.changeQty();
        });
        //温度信息录入
        $('#btnTemperature').on("click", function () {
            page.openTemperature();
        });
    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult, isdetail) {
        clearMessage();
        if (iscondition) {
            $('#txtScanContainer').textbox('clear');
            $('#txtScanMfgorder').textbox('clear');
            $('#txtContainer').textbox('clear');
        }
        if (isresult) {
            clearEasyuiDataGrid("gdContainerInfo", 1);
            clearEasyuiDataGrid("gdContainerInfo2", 1);
            clearEasyuiDataGrid("gdChildContainer", 1);
        }
        if (isdetail) {
            $('#txtDPH').textbox('clear');
            $('#txtSDS').numberbox('clear');
        }
    },
    //查询
    searchData: function () {
        //console.log(new Date());
        page.reset(false, true, true);
        //console.log(new Date());
        var gridID = "#gdContainerInfo";
        var param = {
            action: "searchData",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            containerName: $("#txtContainer").textbox('getValue'),
            scanContainerName: $("#txtScanContainer").textbox('getValue'),
            scanMfgorderName: $("#txtScanMfgorder").textbox('getValue'),
            pageType: page.pageType,
            processNode: page.processNode
        };
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContainerMaterialHandler.ashx",
            data: param,
            cashe: false,
            async: true,
            beforeSend: function () {
                $(gridID).datagrid('loading');
                $(this).attr("disabled", "disabled"); //禁用按钮
            },
            success: function (data) {
                $(gridID).datagrid('loaded');
                $(this).removeAttr("disabled"); //恢复按钮
                var result = JSON.parse(data);
                //给分页对象赋值
                page.pagetotal = result.RowCount;
                $(gridID).datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $(gridID).datagrid('loaded');
                $(this).removeAttr("disabled"); //恢复按钮
                alert(XMLHttpRequest.responseText);
            }
        });
    }
    //查询批次明细
    , searchDataDetail: function () {
        //清除数据
        clearEasyuiDataGrid("gdContainerInfo2", 1);
        clearEasyuiDataGrid("gdChildContainer", 1);
        page.reset(false, false, true);
        var selected = $("#gdContainerInfo").datagrid('getSelected');

        var gridID = "#gdContainerInfo2";
        var param = {
            action: "searchDataDetail",
            productBatchReal: selected.PRODUCTBATCHHIDDEN,
            containerType: selected.CONTAINERTYPE,
            togetherflag: selected.TOGETHERFLAG,
            pageType: page.pageType,
            processNode: page.processNode
        };

        //数据请求
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContainerMaterialHandler.ashx",
            data: param,
            cashe: false,
            async: true,
            beforeSend: function () {
                $(gridID).datagrid('loading');
            },
            success: function (data) {
                $(gridID).datagrid('loaded');
                var result = JSON.parse(data);
                $(gridID).datagrid('loadData', { total: result.length, rows: result });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $(gridID).datagrid('loaded');
                alert(XMLHttpRequest.responseText);
            }
        });
    }
    //查询批次子序号
    , searchMaterialChildData: function (rowData) {
        clearEasyuiDataGrid("gdChildContainer", 1);
        var gridID = "#gdChildContainer";
        var param = {
            action: "searchMaterialChildData",
            containerID: rowData.CONTAINERID,
            pageType: page.pageType,
            processNode: page.processNode
        };

        //数据请求
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContainerMaterialHandler.ashx",
            data: param,
            cashe: false,
            async: true,
            beforeSend: function () {
                $(gridID).datagrid('loading');
            },
            success: function (data) {
                $(gridID).datagrid('loaded');
                var result = JSON.parse(data);
                $(gridID).datagrid('loadData', { total: result.length, rows: result });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $(gridID).datagrid('loaded');
                alert(XMLHttpRequest.responseText);
            }
        });
    }
    //数据确认按钮
    , confirmData: function () {
        clearMessage();
        var selectData = $("#gdContainerInfo2").datagrid('getChecked');
        if (selectData.length == 0) {
            displayMessage("请选择数据", false);
            return;
        }
        var selectMainData = $("#gdContainerInfo").datagrid('getSelected');
        //console.log(selectMainData);
        //数据验证
        for (var i = 0; i < selectData.length; i++) {
            //console.log(selectData[i]);
            if (isNullOrWhiteSpaceStr(selectData[i].MFGWORKFLOWID)) {
                displayMessage("加工工艺路线没有值", false);
                return;
            }
            //温度确认时需要已维护温度信息
            if (page.processNode == "温度确认" && selectData[i].ISHAVETEMPERA == "否") {
                displayMessage("请先录入温度信息", false);
                return;
            }
            //温度确认,到料确认时的验证
            if (page.processNode == "到料确认" || page.processNode == "温度确认") {
                if (isNullOrWhiteSpaceStr(selectData[i].JYBH)) {
                    displayMessage("物资部未发料", false);
                    return;
                }
            }
        }
        //数据提交
        var param = {
            action: "confirmData",
            selectMainData: "[" + JSON.stringify(selectMainData) + "]",
            selectData: JSON.stringify(selectData),
            pageType: page.pageType,
            processNode: page.processNode
        };
        if (page.processNode == "物料检验")//检验确认时，可以更改段批号
        {
            param.ypContainerNo = $("#txtDPH").textbox('getValue');
        }
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContainerMaterialHandler.ashx",
            data: param,
            cashe: false,
            async: true,
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
                // $(this).attr("disabled", "disabled"); //禁用按钮
            },
            success: function (data) {
                $.messager.progress('close');
                // $(this).removeAttr("disabled"); //恢复按钮
                var result = JSON.parse(data);
                if (result.Result) {
                    //刷新列表
                    page.searchData();
                    displayMessage("确认成功", true);
                } else {
                    displayMessage(result.Message, false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // $(this).removeAttr("disabled"); //恢复按钮
                $.messager.progress('close');
                alert(XMLHttpRequest.responseText);
            }
        });


    }
    //查看流水卡
    , viewContainerPrint: function () {
        clearMessage();
        //        var selectData = $("#gdContainerInfo2").datagrid('getChecked');
        //        if (selectData.length == 0) {
        //            displayMessage("请选择数据", false);
        //            return;
        //        }
        //        var ypType = selectData[0].YPCONTAINERTYPE;
        //        var url = "";
        //        if (ypType == "1") {
        //            url = "/YePian/ContainerForgePrintForm.aspx"
        //        } else if (ypType == "2") {
        //            url = "/YePian/ContainerMachiningPrintForm.aspx"
        //        } else if (ypType == "3") {
        //            url = "/YePian/YPXianFengPiContainerInfoPrintForm.aspx"
        //        } else if (ypType == "4") {
        //            url = "/YePian/ContainerSpecialPrintForm.aspx"
        //        }
        //        url += "?ContainerId=" + selectData[0].CONTAINERID + "&WorkflowId=" + selectData[0].MFGWORKFLOWID + "&IsBlank=true";
        //        window.open(page.rootPath + "/forms/Custom" + url);
        var selected = $("#gdContainerInfo2").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("请选择批次", false);
            return;
        }
        for (var i = 0; i < selected.length; i++) {
            selected[i].WORKFLOWID = selected[i].MFGWORKFLOWID;
        }
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/CommonHandler.ashx",
            data: { action: "viewPrintPage", params: JSON.stringify(selected) },
            cashe: false,
            async: true,
            success: function (data) {
                var result = JSON.parse(data);
                if (result.Result) {
                    var url = result.ReturnData + "?v=" + new Date().getTime();
                    window.open(url);
                } else {
                    displayMessage(result.Message, false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText);
            }
        });
    }
    //修改工艺路线
    , changeWorkflow: function () {
        clearMessage();
        var selectData = $("#gdContainerInfo2").datagrid('getChecked');
        if (selectData.length == 0) {
            displayMessage("请选择数据", false);
            return;
        }
        var selectMainData = $("#gdContainerInfo").datagrid('getSelected');
        //console.log(selectMainData);
        //console.log(selectData[0]);
        //数据提交
        var param = {
            action: "changeWorkflow",
            selectData: JSON.stringify(selectData),
            pageType: page.pageType,
            processNode: page.processNode
        };
        //return;
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContainerMaterialHandler.ashx",
            data: param,
            cashe: false,
            async: true,
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });

            },
            success: function (data) {
                $.messager.progress('close');
                var result = JSON.parse(data);
                if (result.Result) {

                    $('#dg').dialog({
                        title: '修改工艺路线',
                        width: 800,
                        height: 600,
                        top: 10,
                        closed: false,
                        cache: false,
                        modal: true,
                        content: '<iframe src="' + page.rootPath + '/forms/Custom/MaterialSetupMainChangeWorkflowPopupForm.aspx" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
                        onClose: function () {
                            var returnValue = $("#dg").dialog('options').returnData;
                            if (returnValue) {
                                page.searchData();
                            }
                        }
                    });

                    //displayMessage("确认成功", true);
                } else {
                    displayMessage(result.Message, false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // $(this).removeAttr("disabled"); //恢复按钮
                $.messager.progress('close');
                alert(XMLHttpRequest.responseText);
            }
        });

    }
    //更改批次数量
    , changeQty: function () {
        clearMessage();
        var selectData = $("#gdContainerInfo2").datagrid('getChecked');
        if (selectData.length == 0) {
            displayMessage("请选择数据", false);
            return;
        }
        //数据验证
        for (var i = 0; i < selectData.length; i++) {
            if (selectData[i].CONTAINERTYPE != "2") {
                displayMessage("只能修改批量流水卡的数量", false);
                return;
            }
        }
        var changeQty = $("#txtSDS").numberbox('getValue');
        if (isNullOrWhiteSpaceStr(changeQty.toString())) {
            displayMessage("请输入实到数量", false);
            return;
        }
        //数据提交
        var param = {
            action: "changeContainerQty",
            selectData: JSON.stringify(selectData),
            changeQty: changeQty,
            pageType: page.pageType,
            processNode: page.processNode
        };
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContainerMaterialHandler.ashx",
            data: param,
            cashe: false,
            async: true,
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
                // $(this).attr("disabled", "disabled"); //禁用按钮
            },
            success: function (data) {
                $.messager.progress('close');
                // $(this).removeAttr("disabled"); //恢复按钮
                var result = JSON.parse(data);
                if (result.Result) {
                    //刷新列表
                    page.searchData();
                    displayMessage("数量修改成功", true);
                } else {
                    displayMessage(result.Message, false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // $(this).removeAttr("disabled"); //恢复按钮
                $.messager.progress('close');
                alert(XMLHttpRequest.responseText);
            }
        });
    }
    //打开温度录入页面
    , openTemperature: function () {
        clearMessage();
        var selectData = $("#gdContainerInfo2").datagrid('getChecked');
        if (selectData.length == 0) {
            displayMessage("请选择数据", false);
            return;
        }
        var url = "YPContaienrTemperaturePage.html?v=" + new Date().getTime();
        url += "&containerId=" + selectData[0].CONTAINERID + "&workflowId=" + selectData[0].MFGWORKFLOWID;

        $('#dg2').dialog({
            title: '温度信息录入',
            width: 1200,
            height: 700,
            top: 10,
            closed: false,
            cache: false,
            modal: true,
            content: '<iframe src="' + page.rootPath + '/forms/Custom/YePian/' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
            onClose: function () {
                var returnValue = window.returnValue;
                if (returnValue != null) {
                    if (returnValue.page = "YPContaienrTemperaturePage.html" && returnValue.isSuccess == true) {
                        page.searchData();
                    }
                }
            }
        });

    }
};
$(function () {
    page.init();
});